package com.qfedu.controller;

import com.qfedu.pojo.req.UserReq;
import com.qfedu.pojo.req.UserRoleReq;
import com.qfedu.pojo.resp.BaseResp;
import com.qfedu.service.AdminService;
import io.swagger.annotations.Api;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("admin")
@Api(tags = "后台管理员管理")
public class AdminController {

    @Autowired
    AdminService adminService;

    /**
     * 后台管理员登录的方法
     * @param userReq   后台传入的admin(管理员)的json对象
     * @return  统一返回json信息，包括状态码，提示信息，返回的数据，分页总条数为空(前台不关注)
     */
    @PostMapping("login")
    public BaseResp login(@RequestBody UserReq userReq) {
        return adminService.login(userReq);
    }

    /**
     * 判断管理员是否已登录的方法
     * @return success/error
     */
//    @RequiresPermissions("LoginBackStage")
    @GetMapping("isAuthenticated")
    public BaseResp isAuthenticated() {
        return adminService.isAuthenticated();
    }

    /**
     * 退出登录的方法
     * @return success/error
     */
    @GetMapping("logout")
    public BaseResp logout() {
        return adminService.logout();
    }

    @RequiresPermissions("SelectAdmin")
    @GetMapping("findAllAdmin")
    public BaseResp findAllAdmin(@RequestParam("page")Integer page, @RequestParam("size")Integer size) {
        return adminService.findAllAdmin(page, size);
    }

    @RequiresPermissions("UpdateAdmin")
    @PostMapping("addAdmin")
    public BaseResp addAdmin(@RequestBody UserReq userReq) {
        return adminService.addOrUpdAdmin(userReq);
    }

    @RequiresPermissions("UpdateAdmin")
    @PostMapping("updAdmin")
    public BaseResp updAdmin(@RequestBody UserReq userReq) {
        return adminService.addOrUpdAdmin(userReq);
    }

    @RequiresPermissions("UpdateAdmin")
    @PostMapping("delAdmin")
    public BaseResp delAdmin(@RequestBody UserReq userReq) {
        return adminService.delAdmin(userReq);
    }

    /**
     * 查询所有的角色以及管理员拥有的角色
     * @param map
     * @return
     */
    @PostMapping("findAllRoleAndAdminRole")
    public BaseResp findAllRoleAndAdminRole(@RequestBody Map map) {
        return adminService.findAllRoleAndAdminRole(Integer.parseInt(map.get("id").toString()));
    }

    /**
     * 对管理员进行授予角色操作
     */
    @RequiresPermissions("UpdateToRole")
    @PostMapping("updAdminRole")
    public BaseResp updAdminRole(@RequestBody UserRoleReq userRoleReq) {
        return adminService.updAdminRole(userRoleReq);
    }
}
